package com.hnevc.androiduiclassone8;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.spec.ECField;

public class JAONActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jaon);
        String jsonString = "{\"name\":\"tom\",\"password\":\"123\",\"adress\":{\"country\":\"china\",\"city\":\"changsha\"}}";
        String jsonArrayString = "[23,2673,324,34,455]";
        try {
            //根据JSONSTRING创建对象OBJECT
            JSONObject object = new JSONObject(jsonString);

            String name = object.getString("name");
            Toast.makeText(this,name,Toast.LENGTH_LONG).show();

            JSONObject adress = object.getJSONObject("adress");
            String country = adress.getString("country");
            Toast.makeText(this,"country"+country,Toast.LENGTH_LONG).show();



        } catch (JSONException e) {
            e.printStackTrace();
        }

    try{
        JSONArray array = new JSONArray(jsonArrayString);
        for (int i= 0; i<array.length();i++) {
            int num = array.getInt(i);
            Toast.makeText(this, "" + num, Toast.LENGTH_LONG).show();
        }

        } catch (JSONException e) {
        e.printStackTrace();
    }

    }
}
